//
//  HPMe.m
//
//  Created by 啊  on 16/4/24
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "HPMe.h"
#import "HPChildren.h"


NSString *const kHPMeCredentials = @"credentials";
NSString *const kHPMeStatus = @"status";
NSString *const kHPMeOpenId = @"openId";
NSString *const kHPMePHeadUrl = @"pHeadUrl";
NSString *const kHPMeLoginVerifyId = @"loginVerifyId";
NSString *const kHPMeRealm = @"realm";
NSString *const kHPMePIsProhibit = @"pIsProhibit";
NSString *const kHPMePId = @"pId";
NSString *const kHPMePUnreadNum = @"pUnreadNum";
NSString *const kHPMePGold = @"pGold";
NSString *const kHPMeEmail = @"email";
NSString *const kHPMePShutupTime = @"pShutupTime";
NSString *const kHPMeLastUpdated = @"lastUpdated";
NSString *const kHPMePMoney = @"pMoney";
NSString *const kHPMePMobile = @"pMobile";
NSString *const kHPMeEmailVerified = @"emailVerified";
NSString *const kHPMeChildren = @"Children";
NSString *const kHPMeChallenges = @"challenges";
NSString *const kHPMeCreated = @"created";
NSString *const kHPMeUsername = @"username";


@interface HPMe ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation HPMe

@synthesize credentials = _credentials;
@synthesize status = _status;
@synthesize openId = _openId;
@synthesize pHeadUrl = _pHeadUrl;
@synthesize loginVerifyId = _loginVerifyId;
@synthesize realm = _realm;
@synthesize pIsProhibit = _pIsProhibit;
@synthesize pId = _pId;
@synthesize pUnreadNum = _pUnreadNum;
@synthesize pGold = _pGold;
@synthesize email = _email;
@synthesize pShutupTime = _pShutupTime;
@synthesize lastUpdated = _lastUpdated;
@synthesize pMoney = _pMoney;
@synthesize pMobile = _pMobile;
@synthesize emailVerified = _emailVerified;
@synthesize children = _children;
@synthesize challenges = _challenges;
@synthesize created = _created;
@synthesize username = _username;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.credentials = [self objectOrNilForKey:kHPMeCredentials fromDictionary:dict];
            self.status = [self objectOrNilForKey:kHPMeStatus fromDictionary:dict];
            self.openId = [self objectOrNilForKey:kHPMeOpenId fromDictionary:dict];
            self.pHeadUrl = [self objectOrNilForKey:kHPMePHeadUrl fromDictionary:dict];
            self.loginVerifyId = [self objectOrNilForKey:kHPMeLoginVerifyId fromDictionary:dict];
            self.realm = [self objectOrNilForKey:kHPMeRealm fromDictionary:dict];
            self.pIsProhibit = [[self objectOrNilForKey:kHPMePIsProhibit fromDictionary:dict] boolValue];
            self.pId = [self objectOrNilForKey:kHPMePId fromDictionary:dict];
            self.pUnreadNum = [[self objectOrNilForKey:kHPMePUnreadNum fromDictionary:dict] doubleValue];
            self.pGold = [[self objectOrNilForKey:kHPMePGold fromDictionary:dict] doubleValue];
            self.email = [self objectOrNilForKey:kHPMeEmail fromDictionary:dict];
            self.pShutupTime = [self objectOrNilForKey:kHPMePShutupTime fromDictionary:dict];
            self.lastUpdated = [self objectOrNilForKey:kHPMeLastUpdated fromDictionary:dict];
            self.pMoney = [[self objectOrNilForKey:kHPMePMoney fromDictionary:dict] doubleValue];
            self.pMobile = [self objectOrNilForKey:kHPMePMobile fromDictionary:dict];
            self.emailVerified = [self objectOrNilForKey:kHPMeEmailVerified fromDictionary:dict];
    NSObject *receivedHPChildren = [dict objectForKey:kHPMeChildren];
    NSMutableArray *parsedHPChildren = [NSMutableArray array];
    if ([receivedHPChildren isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedHPChildren) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedHPChildren addObject:[HPChildren modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedHPChildren isKindOfClass:[NSDictionary class]]) {
       [parsedHPChildren addObject:[HPChildren modelObjectWithDictionary:(NSDictionary *)receivedHPChildren]];
    }

    self.children = [NSArray arrayWithArray:parsedHPChildren];
            self.challenges = [self objectOrNilForKey:kHPMeChallenges fromDictionary:dict];
            self.created = [self objectOrNilForKey:kHPMeCreated fromDictionary:dict];
            self.username = [self objectOrNilForKey:kHPMeUsername fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.credentials forKey:kHPMeCredentials];
    [mutableDict setValue:self.status forKey:kHPMeStatus];
    [mutableDict setValue:self.openId forKey:kHPMeOpenId];
    [mutableDict setValue:self.pHeadUrl forKey:kHPMePHeadUrl];
    [mutableDict setValue:self.loginVerifyId forKey:kHPMeLoginVerifyId];
    [mutableDict setValue:self.realm forKey:kHPMeRealm];
    [mutableDict setValue:[NSNumber numberWithBool:self.pIsProhibit] forKey:kHPMePIsProhibit];
    [mutableDict setValue:self.pId forKey:kHPMePId];
    [mutableDict setValue:[NSNumber numberWithDouble:self.pUnreadNum] forKey:kHPMePUnreadNum];
    [mutableDict setValue:[NSNumber numberWithDouble:self.pGold] forKey:kHPMePGold];
    [mutableDict setValue:self.email forKey:kHPMeEmail];
    [mutableDict setValue:self.pShutupTime forKey:kHPMePShutupTime];
    [mutableDict setValue:self.lastUpdated forKey:kHPMeLastUpdated];
    [mutableDict setValue:[NSNumber numberWithDouble:self.pMoney] forKey:kHPMePMoney];
    [mutableDict setValue:self.pMobile forKey:kHPMePMobile];
    [mutableDict setValue:self.emailVerified forKey:kHPMeEmailVerified];
    NSMutableArray *tempArrayForChildren = [NSMutableArray array];
    for (NSObject *subArrayObject in self.children) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForChildren addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForChildren addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForChildren] forKey:kHPMeChildren];
    [mutableDict setValue:self.challenges forKey:kHPMeChallenges];
    [mutableDict setValue:self.created forKey:kHPMeCreated];
    [mutableDict setValue:self.username forKey:kHPMeUsername];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.credentials = [aDecoder decodeObjectForKey:kHPMeCredentials];
    self.status = [aDecoder decodeObjectForKey:kHPMeStatus];
    self.openId = [aDecoder decodeObjectForKey:kHPMeOpenId];
    self.pHeadUrl = [aDecoder decodeObjectForKey:kHPMePHeadUrl];
    self.loginVerifyId = [aDecoder decodeObjectForKey:kHPMeLoginVerifyId];
    self.realm = [aDecoder decodeObjectForKey:kHPMeRealm];
    self.pIsProhibit = [aDecoder decodeBoolForKey:kHPMePIsProhibit];
    self.pId = [aDecoder decodeObjectForKey:kHPMePId];
    self.pUnreadNum = [aDecoder decodeDoubleForKey:kHPMePUnreadNum];
    self.pGold = [aDecoder decodeDoubleForKey:kHPMePGold];
    self.email = [aDecoder decodeObjectForKey:kHPMeEmail];
    self.pShutupTime = [aDecoder decodeObjectForKey:kHPMePShutupTime];
    self.lastUpdated = [aDecoder decodeObjectForKey:kHPMeLastUpdated];
    self.pMoney = [aDecoder decodeDoubleForKey:kHPMePMoney];
    self.pMobile = [aDecoder decodeObjectForKey:kHPMePMobile];
    self.emailVerified = [aDecoder decodeObjectForKey:kHPMeEmailVerified];
    self.children = [aDecoder decodeObjectForKey:kHPMeChildren];
    self.challenges = [aDecoder decodeObjectForKey:kHPMeChallenges];
    self.created = [aDecoder decodeObjectForKey:kHPMeCreated];
    self.username = [aDecoder decodeObjectForKey:kHPMeUsername];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_credentials forKey:kHPMeCredentials];
    [aCoder encodeObject:_status forKey:kHPMeStatus];
    [aCoder encodeObject:_openId forKey:kHPMeOpenId];
    [aCoder encodeObject:_pHeadUrl forKey:kHPMePHeadUrl];
    [aCoder encodeObject:_loginVerifyId forKey:kHPMeLoginVerifyId];
    [aCoder encodeObject:_realm forKey:kHPMeRealm];
    [aCoder encodeBool:_pIsProhibit forKey:kHPMePIsProhibit];
    [aCoder encodeObject:_pId forKey:kHPMePId];
    [aCoder encodeDouble:_pUnreadNum forKey:kHPMePUnreadNum];
    [aCoder encodeDouble:_pGold forKey:kHPMePGold];
    [aCoder encodeObject:_email forKey:kHPMeEmail];
    [aCoder encodeObject:_pShutupTime forKey:kHPMePShutupTime];
    [aCoder encodeObject:_lastUpdated forKey:kHPMeLastUpdated];
    [aCoder encodeDouble:_pMoney forKey:kHPMePMoney];
    [aCoder encodeObject:_pMobile forKey:kHPMePMobile];
    [aCoder encodeObject:_emailVerified forKey:kHPMeEmailVerified];
    [aCoder encodeObject:_children forKey:kHPMeChildren];
    [aCoder encodeObject:_challenges forKey:kHPMeChallenges];
    [aCoder encodeObject:_created forKey:kHPMeCreated];
    [aCoder encodeObject:_username forKey:kHPMeUsername];
}

- (id)copyWithZone:(NSZone *)zone
{
    HPMe *copy = [[HPMe alloc] init];
    
    if (copy) {

        copy.credentials = [self.credentials copyWithZone:zone];
        copy.status = [self.status copyWithZone:zone];
        copy.openId = [self.openId copyWithZone:zone];
        copy.pHeadUrl = [self.pHeadUrl copyWithZone:zone];
        copy.loginVerifyId = [self.loginVerifyId copyWithZone:zone];
        copy.realm = [self.realm copyWithZone:zone];
        copy.pIsProhibit = self.pIsProhibit;
        copy.pId = [self.pId copyWithZone:zone];
        copy.pUnreadNum = self.pUnreadNum;
        copy.pGold = self.pGold;
        copy.email = [self.email copyWithZone:zone];
        copy.pShutupTime = [self.pShutupTime copyWithZone:zone];
        copy.lastUpdated = [self.lastUpdated copyWithZone:zone];
        copy.pMoney = self.pMoney;
        copy.pMobile = [self.pMobile copyWithZone:zone];
        copy.emailVerified = [self.emailVerified copyWithZone:zone];
        copy.children = [self.children copyWithZone:zone];
        copy.challenges = [self.challenges copyWithZone:zone];
        copy.created = [self.created copyWithZone:zone];
        copy.username = [self.username copyWithZone:zone];
    }
    
    return copy;
}


@end
